﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class Form_nhaxuatban : Form
    {
        public Form_nhaxuatban()
        {
            InitializeComponent();
            hienthi();
        }
        
        quanlybansachDataContext db = new quanlybansachDataContext();
        Nxban_bll nxb = new Nxban_bll();
        
        //===========================================================
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            var hienthi = from ht in db.nxbs select ht;
            dgv.DataSource = hienthi;
            dgv.DataBindings.Clear();
        }
        //===========================================================
        private void btthem_Click(object sender, EventArgs e)
        {
            nxb obj = new nxb();
            bool ok = nxb.kiemtranxb(txtmanxb.Text);
            if (ok)
            {

                obj.manxb = txtmanxb.Text;
                obj.ten = txttennxb.Text;
                nxb.themnxb(obj);
                lblerror.Text = "Thêm thành công";
                hienthi();
            }
            else MessageBox.Show("Thêm thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();
           
        }
        //===========================================================
        private void btnhaplai_Click(object sender, EventArgs e)
        {
            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }
        //===========================================================
        private void btsua_Click(object sender, EventArgs e)
        {
            bool ok = nxb.kiemtranxb(txtmanxb.Text);
            if (!ok)
            {
                nxb.sua(txtmanxb.Text, txttennxb.Text);
               
               
                lblerror.Text = "Sửa thành công";
                hienthi();
            }
            else MessageBox.Show("Sửa thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();
        }
        //===========================================================
        private void btxoa_Click(object sender, EventArgs e)
        {
            bool ok = nxb.kiemtranxb(txtmanxb.Text);
            if (!ok)
            {
                nxb.xoa(txtmanxb.Text);
                lblerror.Text = "Xóa thành công";
                hienthi();

            }
            else MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            hienthi(); 
        }
        //===========================================================
        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;//n is the location of the selected line            
            txtmanxb.Text = dgv.Rows[n].Cells[0].Value.ToString();
            txttennxb.Text = dgv.Rows[n].Cells[1].Value.ToString();
        }
        //===========================================================
        private void btthoat_Click(object sender, EventArgs e)
        {

            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }

        private void bttimanxb_Click(object sender, EventArgs e)
        {
            bool ok = nxb.timkiemmanxb(txtmanxb.Text);
            if (ok)
            {
                dgv.DataSource = from n in db.nxbs where n.manxb.Equals(txtmanxb.Text) select n;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
   
        }

        private void bttimtennxb_Click(object sender, EventArgs e)
        {
            bool ok = nxb.timkietennxb(txttennxb.Text);
            if (ok)
            {
                dgv.DataSource = from n in db.nxbs where n.ten.Equals(txttennxb.Text) select n;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        //===========================================================

    }
}
